//index.js
const app = getApp()
const Api = require("../../../utils/api.js").API
const Store = require("../../../utils/store.js").store
const codeZ = require("../../../utils/util.js")

Component({
    properties: {
        listArr: Object,
        allAmt: Number,
        payType: String,
        isShow: Boolean,
        msg: String,
        priceAmt: Number,
        groupID: String,
        zhiying: String
    },
    data: {
        goods: [],
        showMore: false,
        allAmt: 0.00,
        ErShow: false
    },
    ready() {

    },

    methods: {
        GoZhuFu() {
            this.goGround();
        },
        onClose() {
            var jiag = `priceAmt`;
            this.setData({
                [jiag]: 0,
                groupID: '',
            })
        },
        deleteCart(e) {
            let that = this;
            console.log(e)
            let zhi = e.currentTarget.dataset.item.id
            let i = e.currentTarget.dataset.index;
            var dat = this.properties.listArr;
            dat.splice(i, 1),

                console.log(dat)
            var comSta = `listArr`;
            var jiag = `allAmt`;
            let arg1 = codeZ.allSum(dat);
            let arg2 = this.properties.priceAmt;


            var r1 = codeZ.deal(arg1);
            var r2 = codeZ.deal(arg2);
            var m = Math.pow(10, Math.max(r1, r2))
            var jiage = Number((arg1 * m - arg2 * m) / m).toFixed(2)

            if (jiage < 0) {
                jiage = 0;
            }
            if (dat.length == 0) {
                this.setData({
                    [comSta]: dat,
                    isShow: false
                })
            } else {
                this.setData({
                    [comSta]: dat,
                    [jiag]: jiage
                })
            }
            let a = that.properties.zhiying;
            Api.cartDeleteApi({
                'cartId': zhi
            }).then(res => {
                if (dat.length == 0) {
                    if (a == '111402') {

                        that.triggerEvent('NoCarts2', {}, {});
                    } else {
                        that.triggerEvent('NoCarts', {}, {});
                    }
                }
            })
        },

        clickDe: function(e) {

            let i = e.currentTarget.dataset.index;
            var comSta = `listArr[${i}].isCheak`;
            let a = this.properties.listArr[i].isCheak;
            if (a == true) {
                this.setData({
                    [comSta]: false,


                })
            } else {
                this.setData({
                    [comSta]: true
                })
            }
            let arg1 = codeZ.allSum(this.properties.listArr);
            let arg2 = this.properties.priceAmt;

            var r1 = codeZ.deal(arg1);
            var r2 = codeZ.deal(arg2);
            var m = Math.pow(10, Math.max(r1, r2))
            var aa = Number((arg1 * m - arg2 * m) / m).toFixed(2)

            if (aa < 0) {
                aa = 0.00
            }
            this.setData({
                allAmt: aa
            })

            console.log(this.properties.listArr);

        },
        jumpCoupons: function() {

            if (this.properties.priceAmt > 0) {
                this.setData({
                    ErShow: true
                })

            } else {
                this.goGround();
            }
        },
        goGround() {
            var arr = this.properties.listArr;

            let azhi = [];
            arr.forEach(item => {
                if (item.isCheak) {
                    azhi.push(item);
                }
            })
            let arg1 = codeZ.allSum(azhi);

            let a = {
                'type': this.properties.zhiying,
                'allAmt': arg1
            }

            wx.navigateTo({
                url: '../MyCoupons/index',
                success: function(res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', {
                        data: a
                    })
                }
            })
        },
        listToggle: function() {
            this.setData({
                showMore: !this.data.showMore
            })
        },
        zhifu: function(e) {
            let that = this;
            let arr = this.properties.listArr;
            let cartIDs = [];
            arr.forEach(item => {
                if (item.isCheak == true) {
                    cartIDs.push(item['id']);
                }
            })
            let data = {
                "cartIds": cartIDs,
                "ckCouponId": this.data.groupID,
                "sourceGoods": this.properties.zhiying,
                "openId": wx.getStorageSync('openId')
            }
            Api.order_saveApi(data).then(resp => {
                if (resp.code == 0) {
                    that.wxPay(resp.weChatAppletOrder);
                } else {
                    wx.showToast({
                        title: resp.msg
                    })
                }

            })
        },


        wxPay: function(weChatAppletOrder) {

            var that = this;
            wx.requestPayment({
                timeStamp: weChatAppletOrder.timeStamp,
                nonceStr: weChatAppletOrder.nonceStr,
                package: weChatAppletOrder.pay_package,
                signType: weChatAppletOrder.signType,
                paySign: weChatAppletOrder.paySign,
                success: function(res) {

                    wx.showToast({
                        title: '支付成功'
                    })
                    wx.navigateTo({
                        url: '../order/index',
                    })
                }
            })
        },




    }



})
